home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 3 / adb / i-c < prev    next >
Text File  |  1996-02-12  |  12KB  |  420 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                         I N T E R F A C E S . C                          --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.9 $                              --
  10. --                                                                          --
  11. --   Copyright (C) 1992,1993,1994,1995,1996 Free Software Foundation, Inc.  --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with System;
  37. with Unchecked_Conversion;
  38.  
  39. package body Interfaces.C is
  40.  
  41.    --  The following bodies are temporary, see documentation in spec ???
  42.  
  43.    function To_C (Item : Character) return char is
  44.    begin
  45.       return Character_To_char (Item);
  46.    end To_C;
  47.  
  48.    function To_Ada (Item : char) return Character is
  49.    begin
  50.       return char_To_Character (Item);
  51.    end To_Ada;
  52.  
  53.    function To_C (Item : in Wide_Character) return wchar_t is
  54.    begin
  55.       return Wide_Character_To_wchar_t (Item);
  56.    end To_C;
  57.  
  58.    function To_Ada (Item : in wchar_t) return Wide_Character is
  59.    begin
  60.       return wchar_t_To_Wide_Character (Item);
  61.    end To_Ada;
  62.  
  63.    -----------------------
  64.    -- Is_Nul_Terminated --
  65.    -----------------------
  66.  
  67.    --  Case of char_array
  68.  
  69.    function Is_Nul_Terminated (Item : in char_array) return Boolean is
  70.    begin
  71.       for J in Item'Range loop
  72.          if Item (J) = nul then
  73.             return True;
  74.          end if;
  75.       end loop;
  76.  
  77.       return False;
  78.    end Is_Nul_Terminated;
  79.  
  80.    --  Case of wchar_array
  81.  
  82.    function Is_Nul_Terminated (Item : in wchar_array) return Boolean is
  83.    begin
  84.       for J in Item'Range loop
  85.          if Item (J) = wide_nul then
  86.             return True;
  87.          end if;
  88.       end loop;
  89.  
  90.       return False;
  91.    end Is_Nul_Terminated;
  92.  
  93.    ------------
  94.    -- To_Ada --
  95.    ------------
  96.  
  97.    --  Convert char_array to String (function form)
  98.  
  99.    function To_Ada
  100.      (Item     : in char_array;
  101.       Trim_Nul : in Boolean := True)
  102.       return     String
  103.    is
  104.       Count : Natural;
  105.       From  : size_t;
  106.  
  107.    begin
  108.       if Trim_Nul then
  109.          From := Item'First;
  110.  
  111.          loop
  112.             exit when Item (From) = nul;
  113.  
  114.             if From = Item'Last then
  115.                raise Terminator_Error;
  116.             else
  117.                From := From + 1;
  118.             end if;
  119.          end loop;
  120.  
  121.          Count := Natural (From - Item'First);
  122.  
  123.       else
  124.          Count := Item'Length;
  125.       end if;
  126.  
  127.       declare
  128.          subtype Return_Type is String (1 .. Count);
  129.          type Return_Type_Ptr is access Return_Type;
  130.          function To_Return_Type_Ptr is
  131.            new Unchecked_Conversion (System.Address, Return_Type_Ptr);
  132.  
  133.       begin
  134.          return To_Return_Type_Ptr (Item'Address).all;
  135.       end;
  136.    end To_Ada;
  137.  
  138.    --  Convert char_array to String (procedure form)
  139.  
  140.    procedure To_Ada
  141.      (Item       : in char_array;
  142.       Target     : out String;
  143.       Count      : out Natural;
  144.       Trim_Nul   : in Boolean := True)
  145.    is
  146.       From   : size_t;
  147.  
  148.    begin
  149.       if Trim_Nul then
  150.          From := Item'First;
  151.          loop
  152.             exit when Item (From) = nul;
  153.  
  154.             if From = Item'Last then
  155.                raise Terminator_Error;
  156.             else
  157.                From := From + 1;
  158.             end if;
  159.          end loop;
  160.  
  161.          Count := Natural (From - Item'First);
  162.  
  163.       else
  164.          Count := Item'Length;
  165.       end if;
  166.  
  167.       if Count > Target'Length then
  168.          raise Constraint_Error;
  169.  
  170.       else
  171.          From := Item'First;
  172.          for To in Target'Range loop
  173.             Target (To) := Character (Item (From));
  174.             From := From + 1;
  175.          end loop;
  176.       end if;
  177.  
  178.    end To_Ada;
  179.  
  180.    --  Convert wchar_array to Wide_String (function form)
  181.  
  182.    function To_Ada
  183.      (Item     : in wchar_array;
  184.       Trim_Nul : in Boolean := True)
  185.       return     Wide_String
  186.    is
  187.       Count : Natural;
  188.       From  : size_t;
  189.  
  190.    begin
  191.       if Trim_Nul then
  192.          From := Item'First;
  193.  
  194.          loop
  195.             exit when Item (From) = wide_nul;
  196.  
  197.             if From = Item'Last then
  198.                raise Terminator_Error;
  199.             else
  200.                From := From + 1;
  201.             end if;
  202.          end loop;
  203.  
  204.          Count := Natural (From - Item'First);
  205.  
  206.       else
  207.          Count := Item'Length;
  208.       end if;
  209.  
  210.       declare
  211.          subtype Return_Type is Wide_String (1 .. Count);
  212.          type Return_Type_Ptr is access Return_Type;
  213.          function To_Return_Type_Ptr is
  214.            new Unchecked_Conversion (System.Address, Return_Type_Ptr);
  215.  
  216.       begin
  217.          return To_Return_Type_Ptr (Item'Address).all;
  218.       end;
  219.    end To_Ada;
  220.  
  221.    --  Convert wchar_array to Wide_String (procedure form)
  222.  
  223.    procedure To_Ada
  224.      (Item       : in wchar_array;
  225.       Target     : out Wide_String;
  226.       Count      : out Natural;
  227.       Trim_Nul   : in Boolean := True)
  228.    is
  229.       From   : size_t;
  230.  
  231.    begin
  232.       if Trim_Nul then
  233.          From := Item'First;
  234.          loop
  235.             exit when Item (From) = wide_nul;
  236.  
  237.             if From = Item'Last then
  238.                raise Terminator_Error;
  239.             else
  240.                From := From + 1;
  241.             end if;
  242.          end loop;
  243.  
  244.          Count := Natural (From - Item'First);
  245.  
  246.       else
  247.          Count := Item'Length;
  248.       end if;
  249.  
  250.       if Count > Target'Length then
  251.          raise Constraint_Error;
  252.  
  253.       else
  254.          From := Item'First;
  255.          for To in Target'Range loop
  256.             Target (To) := Wide_Character (Item (From));
  257.             From := From + 1;
  258.          end loop;
  259.       end if;
  260.  
  261.    end To_Ada;
  262.  
  263.    ----------
  264.    -- To_C --
  265.    ----------
  266.  
  267.    --  Convert String to char_array (function form)
  268.  
  269.    function To_C
  270.      (Item       : in String;
  271.       Append_Nul : in Boolean := True)
  272.       return       char_array
  273.    is
  274.    begin
  275.       --  If appending null, we have to make a copy
  276.  
  277.       if Append_Nul then
  278.          declare
  279.             Target : char_array (0 .. Item'Length);
  280.             To     : size_t;
  281.  
  282.          begin
  283.             To := 0;
  284.             for From in Item'Range loop
  285.                Target (To) := char (Item (From));
  286.                To := To + 1;
  287.             end loop;
  288.  
  289.             Target (Item'Length) := nul;
  290.             return Target;
  291.          end;
  292.  
  293.       --  If not appending null, we can use unchecked conversion to return
  294.       --  the result, since we know in GNAT there is structural equivalence.
  295.  
  296.       else
  297.          declare
  298.             subtype Return_Type is char_array (0 .. Item'Length - 1);
  299.             type Return_Type_Ptr is access Return_Type;
  300.             function To_Return_Type_Ptr is
  301.               new Unchecked_Conversion (System.Address, Return_Type_Ptr);
  302.  
  303.          begin
  304.             return To_Return_Type_Ptr (Item'Address).all;
  305.          end;
  306.       end if;
  307.    end To_C;
  308.  
  309.    --  Convert String to char_array (procedure form)
  310.  
  311.    procedure To_C
  312.      (Item       : in String;
  313.       Target     : out char_array;
  314.       Count      : out size_t;
  315.       Append_Nul : in  Boolean := True)
  316.    is
  317.       To : size_t;
  318.  
  319.    begin
  320.       if Target'Length < Item'Length then
  321.          raise Constraint_Error;
  322.  
  323.       else
  324.          To := Target'First;
  325.          for From in Item'Range loop
  326.             Target (To) := char (Item (From));
  327.             To := To + 1;
  328.          end loop;
  329.  
  330.          Count := Item'Length;
  331.  
  332.          if Append_Nul then
  333.             if To > Target'Last then
  334.                raise Constraint_Error;
  335.             else
  336.                Target (To) := nul;
  337.                Count := Count + 1;
  338.             end if;
  339.          end if;
  340.       end if;
  341.    end To_C;
  342.  
  343.    --  Convert Wide_String to wchar_array (function form)
  344.  
  345.    function To_C
  346.      (Item       : in Wide_String;
  347.       Append_Nul : in Boolean := True)
  348.       return       wchar_array
  349.    is
  350.    begin
  351.       --  If appending null, we have to make a copy
  352.  
  353.       if Append_Nul then
  354.          declare
  355.             Target : wchar_array (0 .. Item'Length);
  356.             To     : size_t;
  357.  
  358.          begin
  359.             To := 0;
  360.             for From in Item'Range loop
  361.                Target (To) := wchar_t (Item (From));
  362.                To := To + 1;
  363.             end loop;
  364.  
  365.             Target (Item'Length) := wide_nul;
  366.             return Target;
  367.          end;
  368.  
  369.       --  If not appending null, we can use unchecked conversion to return
  370.       --  the result, since we know in GNAT there is structural equivalence.
  371.  
  372.       else
  373.          declare
  374.             subtype Return_Type is wchar_array (0 .. Item'Length - 1);
  375.             type Return_Type_Ptr is access Return_Type;
  376.             function To_Return_Type_Ptr is
  377.               new Unchecked_Conversion (System.Address, Return_Type_Ptr);
  378.  
  379.          begin
  380.             return To_Return_Type_Ptr (Item'Address).all;
  381.          end;
  382.       end if;
  383.    end To_C;
  384.  
  385.    --  Convert Wide_String to wchar_array (procedure form)
  386.  
  387.    procedure To_C
  388.      (Item       : in Wide_String;
  389.       Target     : out wchar_array;
  390.       Count      : out size_t;
  391.       Append_Nul : in  Boolean := True)
  392.    is
  393.       To : size_t;
  394.  
  395.    begin
  396.       if Target'Length < Item'Length then
  397.          raise Constraint_Error;
  398.  
  399.       else
  400.          To := Target'First;
  401.          for From in Item'Range loop
  402.             Target (To) := wchar_t (Item (From));
  403.             To := To + 1;
  404.          end loop;
  405.  
  406.          Count := Item'Length;
  407.  
  408.          if Append_Nul then
  409.             if To > Target'Last then
  410.                raise Constraint_Error;
  411.             else
  412.                Target (To) := wide_nul;
  413.                Count := Count + 1;
  414.             end if;
  415.          end if;
  416.       end if;
  417.    end To_C;
  418.  
  419. end Interfaces.C;
  420.